#include "lanqwindow.h"
#include <strstream>
#include "../QClient/QClientInterface.h"
#include <stdlib.h>
#include <strstream>

LanqWindow::LanqWindow(User& myInfo,QWidget* parent): QWidget(parent), ui(new Ui::LanqWindow)
{
    ui->setupUi(this);
    this->setWindowTitle(QString("Lan-Q"));
    this->setWindowIcon(QIcon("./ico/logo16.png"));
    if(myInfo.gender == male)
        ui->headLabel->setPixmap(QPixmap("./ico/male48.png"));
    else
        ui->headLabel->setPixmap(QPixmap("./ico/female48.png"));
    string myname = myInfo.name;
    string myip = myInfo.ip;
    Gender gender = myInfo.gender;
    ui->nameLabel->setText(QString::fromStdString(myname));
    if(myInfo.status == stealth)
        ui->statusCombo->setCurrentIndex(1);
    else
        ui->statusCombo->setCurrentIndex(0);
    // test -----------------------------------------------------------
    vector<User> list;
    int count = 10;
 //   while(1){
//        for(int i = 0; i< count; i++){
//            User user;
//            strstream ss;
//            ss << i;
//            string str_i;
//            ss >> str_i;
//            user.name = "Paul ";
//            user.name.append(str_i);
//            user.ip = str_i;
//            user.gender = male;
//            list.push_back(user);
//        }
//        updateUserList(list);
//        cout << "update once\n";
//        count += 5;
//        //sleep(4);
//        list.clear();
//    }
    // end test--------------------------------------------------------
    connect(ui->userList,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(listDoubleClick(QListWidgetItem*)));
    connect(ui->statusCombo, SIGNAL(currentIndexChanged(int)),this,SLOT(statusChanged(int)));
}

LanqWindow::~LanqWindow()
{
    delete ui;
}

void LanqWindow::counterPartSay(User &counterpart, string &msg)
{
    // first search the win_map to see if there's already existed window for this counterpart
    map<string,TalkWindow*>::iterator iter;

    string key = counterpart.ip;
    key.append(" ");
    string s_pid;
    strstream ss;
    ss << counterpart.pid;
    ss >> s_pid;
    key.append(s_pid);

    iter = win_map.find(key);

    QString q_msg = QString::fromStdString(msg);

    if(iter==win_map.end()){    // no window exists
        TalkWindow* tw = new TalkWindow(counterpart);
        tw->show();
        //key = counterpart.ip + " " + counterpart.pid;
        win_map[key] = tw;
        win_status_map[key] = true;
        tw->showCounterpartMsg(q_msg);
    }else{  // the window is already exist
        // see if the window has been closed
        bool opened = win_status_map[key];
        if(opened){
            TalkWindow* tw = win_map[key];
            tw->showCounterpartMsg(q_msg);
        }else{  // recreate a window
            TalkWindow* tw = new TalkWindow(counterpart);
            tw->show();
            win_map[key] = tw;
            win_status_map[key] = true;
            tw->showCounterpartMsg(q_msg);
        }
    }
}

void LanqWindow::listDoubleClick(QListWidgetItem* item)
{
    int index = ui->userList->currentIndex().row();
    vector<User> user_list = getOnlineUsers();

    // TODO ************************************************
    // Get the counterpart's information according to his ip
    User counterpart = user_list[index];
    // END ************************************************

    string key = counterpart.ip;
    key.append(" ");
    strstream ss;
    string s_pid;
    ss << counterpart.pid;
    ss >> s_pid;
    key.append(s_pid);

    map<string,TalkWindow*>::iterator iter;
    iter = win_map.find(key);

    if(iter==win_map.end()){    // no window exists
        TalkWindow* tw = new TalkWindow(counterpart);
        tw->show();
        win_map[key] = tw;
        win_status_map[key] = true;
    }else{  // the window is already exist
        // see if the window has been closed
        bool opened = win_status_map[key];
        if(opened){
            TalkWindow* tw = win_map[key];
        }else{  // recreate a window
            TalkWindow* tw = new TalkWindow(counterpart);
            tw->show();
            win_map[key] = tw;
            win_status_map[key] = true;
        }
    }
}

void LanqWindow::updateUserList(vector<User> &list)
{
    ui->userList->clear();
    for(int i = 0; i < list.size(); i++){
        User user = list[i];
        QString name = QString::fromStdString(user.name);
        QString ip_tip = QString::fromStdString(user.ip);
        QIcon icon;
        if(user.gender == male)
            icon = QIcon("./ico/male16.png");
        else
            icon = QIcon("./ico/female16.png");
        QListWidgetItem *item = new QListWidgetItem(icon,name,ui->userList);
        item->setToolTip(ip_tip);
        ui->userList->addItem(item);
    }
}

void LanqWindow::statusChanged(int index)
{
    if(index == 0){  // online
        setStatus(online);
    }else{  // stealth
        setStatus(stealth);
    }
}

void LanqWindow::closeEvent(QCloseEvent *event)
{
    event->accept();
}
